<template>
  <div>
    <zm-dialog
      :title="'审批' + dialogTitle"
      :visible.sync="value"
      append-to-body
      size="mini"
      :before-close="dialogClose"
      :close-on-click-modal="false"
    >
      <zm-form :model="form" ref="zmForm" :rules="rules">
        <div class="mb20">
          {{ `${dialogTitle}审批,需要填写${dialogTitle}原因` }}
        </div>
        <zm-form-item prop="remark">
          <zm-input
            v-model="form.remark"
            :rows="4"
            type="textarea"
            :maxlength="300"
            :placeholder="placeHolder"
          ></zm-input>
        </zm-form-item>
      </zm-form>
      <template v-slot:footer class="tr">
        <el-button @click="dialogClose" type="">取消</el-button>
        <el-button @click="submit" type="primary">
          {{ dialogTitle }}
        </el-button>
      </template>
    </zm-dialog>
  </div>
</template>

<script>
import dialogMixin from '@/mixins/dialogMixin'

export default {
  name: 'ApproveInput',
  mixins: [dialogMixin],
  props: {
    type: {
      type: String,
      default: '1'
    }
  },
  data () {
    return {
      form: {
        remark: ''
      },
      rules: {
        remark: [
          {
            required: true,
            message: '请输入原因',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    dialogTitle () {
      return +this.type ? '通过' : '驳回'
    },
    placeHolder () {
      return `审批${this.dialogTitle}`
    }
  },
  created () {
    this.form.remark = this.placeHolder
  },
  methods: {
    async submit () {
      console.log('after-approve', this.type)
      let _valid = false
      this.$refs.zmForm.validate(valid => {
        _valid = valid
      })
      if (!_valid) return
      this.$emit(
        'after-approve',
        JSON.parse(
          JSON.stringify({
            type: this.type,
            remark: this.form.remark || this.placeHolder
          })
        )
      )
    }
  }
}
</script>
